package org.eclipse.mylyn.rememberthemilk.ui;

import org.eclipse.mylyn.internal.provisional.commons.ui.CommonImages;
import org.eclipse.mylyn.rememberthemilk.core.RTMCorePlugin;
import org.eclipse.mylyn.rememberthemilk.ui.editors.RTMTaskEditorPage;
import org.eclipse.mylyn.tasks.ui.TasksUiImages;
import org.eclipse.mylyn.tasks.ui.TasksUiUtil;
import org.eclipse.mylyn.tasks.ui.editors.AbstractTaskEditorPageFactory;
import org.eclipse.mylyn.tasks.ui.editors.TaskEditor;
import org.eclipse.mylyn.tasks.ui.editors.TaskEditorInput;
import org.eclipse.swt.graphics.Image;
import org.eclipse.ui.forms.editor.IFormPage;

public class RTMTaskEditorPageFactory extends AbstractTaskEditorPageFactory
{

	public RTMTaskEditorPageFactory()
	{
		// TODO Auto-generated constructor stub
	}

	private static final String TITLE = "Browser";

//	@Override
//	public boolean canCreateEditorFor(AbstractTask task) {
//		return (task instanceof RTMTask);
//	}
//
//	@Override
//	public boolean canCreateEditorFor(IEditorInput input) {
//		if (input instanceof RepositoryTaskEditorInput) {
//			RepositoryTaskEditorInput taskInput = (RepositoryTaskEditorInput) input;
//			return taskInput.getTaskData() != null
//					&& RTMCorePlugin.REPOSITORY_KIND.equals(taskInput.getRepository().getConnectorKind());
//		} else if (input instanceof TaskEditorInput) {
//			TaskEditorInput taskInput = (TaskEditorInput) input;
//			return taskInput.getTask() instanceof RTMTask;
//		}
//
//		return false;
//	}
//
//	@Override
//	public IEditorPart createEditor(TaskEditor parentEditor, IEditorInput editorInput) {
//		if (editorInput instanceof RepositoryTaskEditorInput) {
//			RepositoryTaskEditorInput taskInput = (RepositoryTaskEditorInput) editorInput;
//			if (taskInput.getTaskData().isNew()) {
//				return new NewRTMTaskEditor(parentEditor);
//			} else {
//				return new RTMTaskEditor(parentEditor);
//			}
//		} else if (editorInput instanceof TaskEditorInput) {
//				return new RTMTaskEditor(parentEditor);
//		}
//		return null;
//	}
//
//	@Override
//	public IEditorInput createEditorInput(AbstractTask task) {
//		RTMTask tracTask = (RTMTask) task;
//		TaskRepository repository = TasksUiPlugin.getRepositoryManager().getRepository(RTMCorePlugin.REPOSITORY_KIND,
//				tracTask.getRepositoryUrl());
//		
//		return new RepositoryTaskEditorInput(repository, tracTask.getTaskId(), tracTask.getUrl());
//		
//	}

	public String getTitle() {
		return "RememberTheMilk";
	}

	public boolean providesOutline() {
		return true;
	}

	@Override
	public boolean canCreatePageFor(TaskEditorInput input) {
		if (input.getTaskRepository().getConnectorKind().equals(RTMCorePlugin.REPOSITORY_KIND)) {
			return true;
		} else if (TasksUiUtil.isOutgoingNewTask(input.getTask(), RTMCorePlugin.REPOSITORY_KIND)) {
			return true;
		}
		return false;
	}

	@Override
	public IFormPage createPage(TaskEditor parentEditor) {
		return new RTMTaskEditorPage(parentEditor);
	}

	@SuppressWarnings("restriction")
	@Override
	public Image getPageImage() {
		return CommonImages.getImage(TasksUiImages.REPOSITORY_SMALL);
	}

	@Override
	public String getPageText() {
		return "Trac"; //$NON-NLS-1$
	}
	
	@Override
	public int getPriority() {
		return AbstractTaskEditorPageFactory.PRIORITY_TASK;
	}
}
